﻿// ----------------------------------------------------------------------------------
// Microsoft Developer & Platform Evangelism
// 
// Copyright (c) Microsoft Corporation. All rights reserved.
// 
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
// EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
// OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
// ----------------------------------------------------------------------------------
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
// ----------------------------------------------------------------------------------

namespace umbraco.ACSExtensions
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web.Configuration;
    using Microsoft.IdentityModel.Web;
    using Microsoft.Samples.DPE.ACS.ServiceManagementWrapper;

    public class AcsManagementService
    {
        private readonly ServiceManagementWrapper wrapper;

        public AcsManagementService()
            : this(WebConfigurationManager.AppSettings["AcsNamespace"], WebConfigurationManager.AppSettings["AcsManagementKey"])
        { 
        }

        public AcsManagementService(string serviceNamespace, string managementKey)
        {
            this.wrapper = new ServiceManagementWrapper(serviceNamespace, "ManagementClient", managementKey);
        }

        public IEnumerable<IdentityProviderInfo> RetrieveBusinessIdentityProviders()
        {
            var identityProviders = new List<IdentityProviderInfo>();

            // Retrieve ONLY the IPs associated with Umbraco RP
            if (FederatedAuthentication.WSFederationAuthenticationModule != null &&
                !(string.IsNullOrWhiteSpace(FederatedAuthentication.WSFederationAuthenticationModule.Realm)))
            {
                identityProviders = this.wrapper.RetrieveBusinessIdentityProviders()
                    .Where(ip => ip.RelyingPartyIdentityProviders.Any(
                        rp =>
                            rp.RelyingParty.RelyingPartyAddresses.Single(a => a.EndpointType.Equals(RelyingPartyAddressEndpointType.Realm.ToString(), StringComparison.OrdinalIgnoreCase))
                                .Address.Equals(FederatedAuthentication.WSFederationAuthenticationModule.Realm, StringComparison.OrdinalIgnoreCase)))
                    .Select(ip => new IdentityProviderInfo { Id = ip.Issuer.Name, Name = ip.DisplayName })
                    .ToList();
            }

            return identityProviders;
        }

        public IEnumerable<string> RetrieveIdentityProviderClaimTypes(string name)
        {
            var claimTypes = new string[0];
            var identityProvider = this.wrapper.RetrieveIdentityProvider(name);

            if (identityProvider != null)
            {
                claimTypes = identityProvider.IdentityProviderClaimTypes.Select(ct => ct.ClaimType.Uri).ToArray();
            }

            return claimTypes;
        }
    }
}